package com.zmcn.msg.business.user.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.zmcn.msg.bean.po.Role;
import com.zmcn.msg.bean.po.User;
import com.zmcn.msg.bean.vo.UserVO;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.dao.BaseDao;

import com.zmcn.msg.business.user.vo.UserExVO;

/**
 * 
 * 用户管理数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class UserDao extends BaseDao {
	/**
	 * 查询用户列表
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<UserExVO> getList(Page<UserExVO> page) {
		String hqlResult = "select new " + UserExVO.class.getName() + UserExVO.STRUCT;
		String hqlCount = "select count(user)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(User.class.getName()).append(" user");
		sb.append(" , ").append(Role.class.getName()).append(" role");
		sb.append(" where user.status=0 and role.roleId=user.roleId");
		List<Object> ps = new ArrayList<Object>();
		for (Iterator<String> item = page.getParas().keySet().iterator(); item.hasNext();) {
			String key = item.next();
			if ("name".equals(key) || "code".equals(key) || "phone".equals(key) || "account".equals(key)) {
				sb.append(" and user.").append(key).append(" like ?");
				ps.add("%" + page.getParas().get(key)[0] + "%");
			} else {
				sb.append(" and user.").append(key).append("=?");
				ps.add(page.getParas().get(key)[0]);
			}
		}
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

	/**
	 * 根据用户ID获取用户信息
	 * 
	 * @param userId
	 *            用户ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public UserVO getUser(String userId) {
		UserVO result = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(UserVO.class.getName()).append(UserVO.STRUCT);
		sb.append(" from ").append(User.class.getName()).append(" user");
		sb.append(" where user.userId=?");
		Query q = getSession().createQuery(sb.toString());
		q.setParameter(0, userId);
		List<UserVO> list = (List<UserVO>) q.list();
		if (list.size() > 0) {
			result = list.get(0);
		}
		return result;
	}

	/**
	 * 根据帐号获取用户信息
	 * 
	 * @param account
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public UserVO getUserByAccount(String account) {
		UserVO result = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(UserVO.class.getName()).append(UserVO.STRUCT);
		sb.append(" from ").append(User.class.getName()).append(" user");
		sb.append(" where user.account=?");
		Query q = getSession().createQuery(sb.toString());
		q.setParameter(0, account);
		List<UserVO> list = q.list();
		if (list.size() > 0) {
			result = list.get(0);
		}
		return result;
	}

	/**
	 * 删除用户
	 * 
	 * @param ids
	 *            用户ID
	 * @param user
	 *            当前操作者
	 */
	public void remove(String[] ids) {
		StringBuffer sb = new StringBuffer();
		sb.append("update ").append(User.class.getName()).append(" u set u.status=1 where");
		for (int i = 0; i < ids.length; i++) {
			sb.append(0 == i ? "" : " or");
			sb.append(" u.userId=?");
		}
		Query q = getSession().createQuery(sb.toString());
		for (int i = 0; i < ids.length; i++) {
			q.setParameter(i, ids[i]);
		}
		q.executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	public List<UserVO> getAllUser() {
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(UserVO.class.getName()).append(UserVO.STRUCT);
		sb.append(" from ").append(User.class.getName()).append(" user");
		Query q = getSession().createQuery(sb.toString());
		List<UserVO> list = (List<UserVO>) q.list();
		return list;
	}
}
